package com.rightsidetech.http.controller.storeApp.stork;

import com.github.pagehelper.PageInfo;
import com.rightsidetech.commom.base.DelReq;
import com.rightsidetech.commom.base.PageRequest;
import com.rightsidetech.commom.base.ResponseData;
import com.rightsidetech.commom.base.TimeQueryRequest;
import com.rightsidetech.commom.enums.CodeIdEnum;
import com.rightsidetech.commom.utils.BeanValidator;
import com.rightsidetech.http.aspect.Log;
import com.rightsidetech.http.aspect.Token;
import com.rightsidetech.http.base.BaseController;
import com.rightsidetech.mall.service.MallCommodityService;
import com.rightsidetech.system.dtos.response.*;
import com.rightsidetech.system.dtos.response.store.SysVerifyReplenishResp;
import com.rightsidetech.system.entity.store.SysStoreLoginInfo;
import com.rightsidetech.system.service.SysStoreStorkService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shenyu.client.springmvc.annotation.ShenyuSpringMvcClient;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.ConstraintViolation;
import java.util.List;

/**
 * @Author: yhr
 * @Date: 2021-12-20 10:34
 */
@Slf4j
@RestController
@Api(tags = "门店app-库存管理")
@RequestMapping("/storeApp/Stock")
@ShenyuSpringMvcClient(path = "/storeApp/Stock/**")
public class StoreAppStockController extends BaseController {
    @Resource
    private SysStoreStorkService sysStoreStorkService;
    @Resource
    private MallCommodityService mallCommodityService;

    @Log
    @Token
    @GetMapping("/listAll")
    @ApiOperation("门店库存")
    public ResponseData<PageInfo<SysStoreStorkAppResp>> listAll(PageRequest req){
        ConstraintViolation validate = BeanValidator.validate(req);
        if (validate != null) {
            return new ResponseData<>(CodeIdEnum.ParameterMalformed.getValue(), validate.getMessage());
        }
        Long loginInfoId = Long.parseLong(getAttribute());
        ResponseData<PageInfo<SysStoreStorkAppResp>> listResponseData = sysStoreStorkService.listAll(req,loginInfoId);
        List<SysStoreStorkAppResp> list = listResponseData.getResData().getList();
        if (!CollectionUtils.isEmpty(list)){
            for (SysStoreStorkAppResp resp:list) {
                resp.setImg(mallCommodityService.selectImg(resp.getCommodityId()));
            }
        }

        return listResponseData;
    }

    @Log
    @Token
    @PostMapping("/verifyReplenish")
    @ApiOperation("待确认库存")
    public ResponseData<SysVerifyReplenishResp> verifyReplenish(){
        Long loginInfoId = Long.parseLong(getAttribute());
        return sysStoreStorkService.verifyReplenish(loginInfoId);
    }


    @Log
    @Token
    @GetMapping("/timeOutStock")
    @ApiOperation("过期商品列表")
    public ResponseData<List<SysStorkDetailsTimeOutResp>> timeOutStock(){
        Long loginInfoId = Long.parseLong(getAttribute());
        SysStoreLoginInfo info = sysStoreStorkService.storeLoginInfo(loginInfoId);
        if (ObjectUtils.isEmpty(info)){
            return ResponseData.fail(CodeIdEnum.EMPTY_DATA);
        }
        return sysStoreStorkService.timeOutStock(info.getStoreId());

    }

    @Log
    @Token
    @GetMapping("/timeOutStockInfo")
    @ApiOperation("过期商品")
    public ResponseData<List<SysStoreStorkDetailsAppResp>> timeOutStockInfo(){
        Long loginInfoId = Long.parseLong(getAttribute());
        SysStoreLoginInfo info = sysStoreStorkService.storeLoginInfo(loginInfoId);
        if (ObjectUtils.isEmpty(info)){
            return ResponseData.fail(CodeIdEnum.EMPTY_DATA);
        }
        ResponseData<List<SysStoreStorkDetailsAppResp>> listResponseData = sysStoreStorkService.timeOutDetails(info.getStoreId());
        for (SysStoreStorkDetailsAppResp resp:listResponseData.getResData()) {
            resp.setImg(mallCommodityService.selectImg(resp.getCommodityId()));
        }
        return listResponseData;

    }

    @Log
    @Token
    @GetMapping("/replenish")
    @ApiOperation("补货记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "verify", value = "是否待确认补货记录 0待确认 1所有" ,required = true)})
    public ResponseData<PageInfo<SysStoreStorkReplenishAppResp>> replenish(Byte verify, TimeQueryRequest timeQueryRequest){
        if (StringUtils.isEmpty(verify)){
            return ResponseData.fail("类型参数不能为空");
        }
        if (verify == 0 || verify == 1){
            Long loginInfoId = Long.parseLong(getAttribute());
            return sysStoreStorkService.listReplenish(loginInfoId, verify,timeQueryRequest);
        }else {
            return ResponseData.fail("类型参数不合法");
        }
    }

    @Log
    @Token
    @PostMapping("/updateVerifyReplenish")
    @ApiOperation("确认补货记录")
    public ResponseData updateVerifyReplenish(@RequestBody DelReq req){
        return sysStoreStorkService.updateVerifyById(req.getId());
    }

    @Log
    @Token
    @PostMapping("/updateVerifyReplenish_batch")
    @ApiOperation("批量确认补货记录")
    public ResponseData updateVerifyReplenish(@RequestBody List<Long> reqList){
        if(reqList.isEmpty()){
            return ResponseData.fail("参数不能为空");
        }
        for(Long id:reqList){
            ResponseData resp = sysStoreStorkService.checkVerifyById(id);
            if(resp.getCodeId() != 1){
                return resp;
            }
        }
        for(Long id:reqList){
            sysStoreStorkService.updateVerifyHavedCheckedById(id);
        }

        return ResponseData.success();
    }

    @Log
    @Token
    @GetMapping("/replenishInfo")
    @ApiOperation("补货详情")
    public ResponseData<List<SysStoreStorkDetailsAppResp>> replenishInfo(Long id){
        if (StringUtils.isEmpty(id)){
            return ResponseData.fail("记录id不能为空");
        }
        ResponseData<List<SysStoreStorkDetailsAppResp>> listResponseData = sysStoreStorkService.listReplenishInfo(id);
        for (SysStoreStorkDetailsAppResp resp:listResponseData.getResData()) {
            resp.setImg(mallCommodityService.selectImg(resp.getCommodityId()));
        }
        return listResponseData;
    }
}
